package ar.com.ironsoft.javaopenauth.web;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.com.ironsoft.javaopenauth.dto.OAuthToken;
import ar.com.ironsoft.javaopenauth.oauth.OAuthService;
import ar.com.ironsoft.javaopenauth.oauth.impl.FacebookOAuthServiceV2;
import ar.com.ironsoft.javaopenauth.oauth.impl.FoursquareOAuthServiceV2;
import ar.com.ironsoft.javaopenauth.oauth.impl.HotmailOAuthServiceWRAP;
import ar.com.ironsoft.javaopenauth.utils.Constants;

/**
 * Simple abstract class to extend HttpServlet for adding a OAuthService
 * property and make visible the doGet and doPost methods.
 * 
 * @author Tomas de Priede
 * @version 0.1.2, 05/02/2011
 * @since 0.1.2-SNAPSHOT
 */
@SuppressWarnings("serial")
public abstract class HttpVisibleServlet extends HttpServlet {

	private OAuthService service;

	/**
	 * Builds a HttpVisibleServlet with one OAuthService
	 * @param service the service to contain
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 */
	public HttpVisibleServlet(OAuthService service) {
		super();
		this.service = service;
	}

	/**
	 * Makes visible the doGet method
	 * @param request The http request
	 * @param response The http response
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 */
	public abstract void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException;

	/**
	 * Makes visible the doPost method
	 * @param request The http request
	 * @param response The http response
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 */
	public abstract void doPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException;

	/**
	 * Returns the oauth service
	 * @return the oauth service
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 */
	public OAuthService getService() {
		return service;
	}

	/**
	 * Sets the oauth service
	 * @param service the service to set
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 */
	public void setService(OAuthService service) {
		this.service = service;
	}
	
	/**
	 * Returns the token associated to the service
	 * @return
	 * @author Tomas de Priede
	 * @version 0.2.1, 06/05/2011
	 * @since 0.2.1-SNAPSHOT
	 */
	public OAuthToken getOAuthToken(HttpServletRequest request){
		Object token = null;
		if (service instanceof FacebookOAuthServiceV2)
			token = request.getSession().getAttribute(Constants.FACEBOOK_SESSION_OAUTH_TOKEN);
		if (service instanceof HotmailOAuthServiceWRAP)
			token = request.getSession().getAttribute(Constants.HOTMAIL_SESSION_OAUTH_TOKEN);
		if (service instanceof FoursquareOAuthServiceV2)
			token = request.getSession().getAttribute(Constants.FOURSQUARE_SESSION_OAUTH_TOKEN);
		if (token != null)
			return (OAuthToken) token;
		return null;
	}

	/**
	 * 
	 * @param request
	 * @param token
	 * @author Tomas de Priede
	 * @version 0.2.1, 06/05/2011
	 * @since 0.2.1-SNAPSHOT
	 */
	public void setOAuthToken(HttpServletRequest request,OAuthToken token){
		if (service instanceof FacebookOAuthServiceV2)
			request.getSession().setAttribute(Constants.FACEBOOK_SESSION_OAUTH_TOKEN,token);
		if (service instanceof HotmailOAuthServiceWRAP)
			request.getSession().setAttribute(Constants.HOTMAIL_SESSION_OAUTH_TOKEN,token);
		if (service instanceof FoursquareOAuthServiceV2)
			request.getSession().setAttribute(Constants.FOURSQUARE_SESSION_OAUTH_TOKEN,token);
	}
}
